//////////////////////////////////////////////////////////////////////////////////////
//
//  Copyright (c) 2014-present, Egret Technology.
//  All rights reserved.
//  Redistribution and use in source and binary forms, with or without
//  modification, are permitted provided that the following conditions are met:
//
//     * Redistributions of source code must retain the above copyright
//       notice, this list of conditions and the following disclaimer.
//     * Redistributions in binary form must reproduce the above copyright
//       notice, this list of conditions and the following disclaimer in the
//       documentation and/or other materials provided with the distribution.
//     * Neither the name of the Egret nor the
//       names of its contributors may be used to endorse or promote products
//       derived from this software without specific prior written permission.
//
//  THIS SOFTWARE IS PROVIDED BY EGRET AND CONTRIBUTORS "AS IS" AND ANY EXPRESS
//  OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
//  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
//  IN NO EVENT SHALL EGRET AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
//  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
//  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;LOSS OF USE, DATA,
//  OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
//  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
//  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
//  EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
//////////////////////////////////////////////////////////////////////////////////////

class LoadingUI extends egret.Sprite{

    public constructor() {
        super();
        this.createView();
    }

    private textField: egret.TextField;
    private textFieldPower: egret.TextField;
    private bg: egret.Bitmap;
    private logo: egret.Bitmap;
    private uiContainer: egret.DisplayObjectContainer;
    private logoUrl = "resource/assets/loading_logo.png";
    private bgUrl = "resource/assets/loading_bg.jpg";
    private w: number;
    private h: number;

    private createView(): void {
        this.w = Const.SENT_WIDTH;
        this.h = Const.SENT_HEIGHT;

        this.textField = new egret.TextField();
        this.textField.y = 500;
        this.textField.textColor = 0x333333;
        this.textField.size = 23;
        this.textField.width = this.w;
        this.textField.height = 100;
        this.textField.fontFamily = "Black";
        this.textField.textAlign = "center";

        this.textFieldPower = new egret.TextField();
        this.textFieldPower.y = this.h * 0.9;
        this.textFieldPower.textColor = 0x333333;
        this.textFieldPower.width = this.w;
        this.textFieldPower.height = 100;
        this.textFieldPower.size = 20;
        this.textFieldPower.text = "powered powerd";
        this.textFieldPower.fontFamily = "Black";
        this.textFieldPower.textAlign = "center";

        let urlLoader: egret.URLLoader = new egret.URLLoader();
        urlLoader.addEventListener(egret.Event.COMPLETE, this.onComplete, this);
        urlLoader.dataFormat = egret.URLLoaderDataFormat.TEXTURE;
        urlLoader.load(new egret.URLRequest(this.logoUrl));

        urlLoader = new egret.URLLoader();
        urlLoader.addEventListener(egret.Event.COMPLETE, this.onComplete, this);
        urlLoader.dataFormat = egret.URLLoaderDataFormat.TEXTURE;
        urlLoader.load(new egret.URLRequest(this.bgUrl));

        this.bg = new egret.Bitmap();
        this.logo = new egret.Bitmap();

        this.uiContainer = new egret.DisplayObjectContainer();

        this.addChild(this.uiContainer);
        this.addChild(this.logo);
        this.addChildAt(this.bg, 0);
        this.addChild(this.textField);
        this.addChild(this.textFieldPower);    
    }

    private onComplete(evt: egret.Event): void
    {
        
        let urlLoader: egret.URLLoader = <egret.URLLoader>evt.target;
        let texture = urlLoader.data;
        switch (urlLoader._request.url)
        {
            case this.bgUrl:
            { 
                let bg = this.bg;
                bg.texture = texture;  //纹理
                bg.scaleX = this.w/640;
                bg.scaleY = this.y/960;   
            }
            break;
            case this.logoUrl:
            {
                let logo = this.logo;
                logo.texture = texture;
                logo.anchorOffsetX = this.logo.width * 0.5;
                logo.anchorOffsetY = this.logo.height * 0.5;
                logo.x = this.w / 2;
                logo.y = this.h / 2;
                logo.scaleX = logo.scaleY = this.h / 960;
                
                this.textField.y = this.logo.y + 100;

            }
            break;
        }
    }

    public setProgress(cur: number, total: number): void
    {
        let num: number = Math.floor((cur / total) * 100);
        this.textField.text = "loading..." + num + "%";
    }

    public onLoadComplete(cb: Function, thisObj): void
    {
        cb.call(thisObj);
    }
    public onProgress(current: number, total: number): void {
        this.textField.text = `Loading...${current}/${total}`;
    }
}
